/**
 * kl yy-ds @2021
 */
package com.cxps.server.vip.controller;

import com.cxps.cloud.message.ApiResponse;
import com.cxps.server.vip.domain.VIPInfo;
import com.cxps.server.vip.domain.VIPInfoQuery;
import com.cxps.server.vip.service.VIPService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 会员模块 前端控制器
 *
 * @author YouCai.Liu
 * @since 2021/11/7
 * ...
 */
@Slf4j
@RestController
@RequestMapping("/admin/vip")
@Api(tags = "会员API")
public class VIPController {

    @Autowired
    private VIPService vipService;

    /**
     * 会员列表
     *
     * @param query
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "会员列表", response = ApiResponse.class)
    public ApiResponse list(@RequestBody VIPInfoQuery query) {
        return ApiResponse.fireSuccess(vipService.list(query));
    }

    /**
     * 新增会员
     *
     * @param info
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增会员", response = ApiResponse.class)
    public ApiResponse add(@RequestParam VIPInfo info) {
        try {
            Assert.notNull(info, "会员信息对象不能为空");
            Assert.hasLength(info.getVipPhone(), "会员手机号不能为空");
            vipService.add(info);
        } catch (IllegalArgumentException e) {
            log.error("新增会员异常：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
        return ApiResponse.fireSuccess();
    }

    /**
     * 修改会员
     *
     * @param info
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改会员", response = ApiResponse.class)
    public ApiResponse update(@RequestParam VIPInfo info) {
        try {
            Assert.notNull(info, "会员信息对象不能为空");
            Assert.hasLength(info.getId(), "会员ID不能为空");
            Assert.hasLength(info.getVipPhone(), "会员手机号不能为空");
            vipService.update(info);
        } catch (IllegalArgumentException e) {
            log.error("修改会员异常：" + e.getMessage(), e);
            return ApiResponse.fireError(e.getMessage());
        }
        return ApiResponse.fireSuccess();
    }

    /**
     * 根据会员ID查询会员信息
     *
     * @param id
     * @return
     */
    @PostMapping("/{id}")
    @ApiOperation(value = "根据会员ID查询会员信息", response = ApiResponse.class)
    public ApiResponse get(@PathVariable String id) {
        return ApiResponse.fireSuccess(vipService.getVipById(id));
    }

    /**
     * 冻结会员
     *
     * @param id
     * @return
     */
    @PostMapping("/freeze/{id}")
    @ApiOperation(value = "冻结会员", response = ApiResponse.class)
    public ApiResponse freeze(@PathVariable String id) {
        vipService.freeze(id);
        return ApiResponse.fireSuccess();
    }

    /**
     * 解冻会员
     *
     * @param id
     * @return
     */
    @PostMapping("/unfreeze/{id}")
    @ApiOperation(value = "解冻会员", response = ApiResponse.class)
    public ApiResponse unfreeze(@PathVariable String id) {
        vipService.unfreeze(id);
        return ApiResponse.fireSuccess();
    }
}
